/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlwriter;

import java.io.FileOutputStream;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.events.Characters;

import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import Model.User;


/**
 *
 * @author Oscar
 */
public class UserWriter {

    private String configFile;

    public void setFile(String configFile) {
            this.configFile = configFile;
    }

    public void saveConfig(List<User> users) throws Exception {

        // Create a XMLOutputFactory
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

        // Create XMLEventWriter
        XMLEventWriter eventWriter = outputFactory
                        .createXMLEventWriter(new FileOutputStream(configFile));

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

        eventWriter.add(eventFactory.createStartElement
                ("", "", "users"));
        eventWriter.add(end);

        for (User user : users) {

            // Create config open tag
            StartElement configStartElement = eventFactory.createStartElement("",
                            "", "user");
            eventWriter.add(configStartElement);
            eventWriter.add(end);

            // Write the different nodes
            createNode(eventWriter, "nickname", user.getNickname());
            createNode(eventWriter, "password", user.getPassword());

            eventWriter.add(eventFactory.createEndElement("", "", "user"));
            eventWriter.add(end);
        }

        eventWriter.add(eventFactory.createEndElement("", "", "users"));

        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }

    private void createNode(XMLEventWriter eventWriter, String name,
                    String value) throws XMLStreamException {

        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        XMLEvent tab = eventFactory.createDTD("\t");
        // Create Start node
        StartElement sElement = eventFactory.createStartElement("", "", name);
        eventWriter.add(tab);
        eventWriter.add(sElement);
        // Create Content
        Characters characters = eventFactory.createCharacters(value);
        eventWriter.add(characters);
        // Create End node
        EndElement eElement = eventFactory.createEndElement("", "", name);
        eventWriter.add(eElement);
        eventWriter.add(end);

    }

}
